#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    serialPort = new QSerialPort(this);
    BorderBar = new QToolBar(this);
    BorderAction = new QAction("图像加框");
    this->addToolBar(BorderBar);
    BorderBar->addAction(BorderAction);
    connect(ui->serialNum, SIGNAL(clicked()), this, SLOT(serialPortFind()));
    connect(serialPort, &QSerialPort::readyRead, this, &MainWindow::serialPort_readyRead);
    connect(BorderAction, &QAction::triggered, this, &MainWindow::addBorder);
    this->setWindowTitle("裁判系统");
}

void MainWindow::serialPortFind()
{
    ui->serialNum->clear();
    foreach (const QSerialPortInfo &inf0, QSerialPortInfo::availablePorts()) {
        serialPortName.append(inf0.portName());
        ui->serialNum->addItem(inf0.portName());
    }

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::serialPort_readyRead()
{
    //从接收缓冲区中读取数据
    QByteArray buffer = serialPort->readAll();

    QRegExp rx4("(\\d+)");
    QString str4 = "Offsets: 12 14 99 231 7";
    QStringList list;
    int pos2 = 0;
    while ((pos2 = rx4.indexIn(QString(buffer), pos2)) != -1)
    {
        list << rx4.cap(1);                                   // 第一个捕获到的文本
        pos2 += rx4.matchedLength();             // 上一个匹配的字符串的长度
    }
    if(list.length()==6){
        ui->time->setText(QString("%1.%2秒").arg(list.at(0).toInt()).arg(list.at(1)));
        ui->coordinate->setText(QString("x=%1\r\ny=%2").arg(list.at(2)).arg(list.at(3)));
        dataHandle(list);
    }
    else{
        ui->Subclass->setText("error");
        ui->Mainclass->setText("error");
        ui->time->setText("error");
        ui->coordinate->setText("error");
    }

    //从界面中读取以前收到的数据
    QString recv = ui->recvTextEdit->toPlainText();
    recv += QString(buffer);
    //清空以前的显示
    ui->recvTextEdit->clear();
    //重新显示
    ui->recvTextEdit->append(recv);
}

void MainWindow::on_open_clicked()
{
    serialPort->setBaudRate(ui->bundary->currentText().toInt());
    serialPort->setDataBits(QSerialPort::Data8);
    serialPort->setStopBits(QSerialPort::OneStop);
    serialPort->setParity(QSerialPort::NoParity);
    serialPort->setPortName(ui->serialNum->currentText());
    if (true == serialPort->open(QIODevice::ReadWrite))
    {
        QMessageBox::information(this, "提示", "串口打开成功");
        ui->close->setEnabled(true);
        ui->open->setEnabled(false);
    }
    else
    {
        QMessageBox::critical(this, "提示", "串口打开失败");
    }

}


void MainWindow::on_close_clicked()
{
        ui->close->setEnabled(false);
        ui->open->setEnabled(true);
        serialPort->close();
}

void MainWindow::dataHandle(QStringList list)
{
    if(list.at(4)=="1")
    {
        ui->Mainclass->setText("动物");
        if(list.at(5)=="1"){
            ui->Subclass->setText("狗");
        }
        else if (list.at(5)=="2") {
            ui->Subclass->setText("马");
        }
        else if (list.at(5)=="3") {
            ui->Subclass->setText("猫");
        }
        else if (list.at(5)=="0") {
            ui->Subclass->setText("没有目标");
        }
        else {
            ui->Subclass->setText("error");
            ui->Mainclass->setText("error");
        }

    }
    else if(list.at(4)=="2")
    {
        ui->Mainclass->setText("水果");
        if(list.at(5)=="1"){
            ui->Subclass->setText("橙子");
        }
        else if (list.at(5)=="2") {
            ui->Subclass->setText("苹果");
        }
        else if (list.at(5)=="3") {
            ui->Subclass->setText("榴莲");
        }
        else if (list.at(5)=="0") {
            ui->Subclass->setText("没有目标");
        }
        else if(list.at(5)=="4"){
            ui->Subclass->setText("葡萄");
        }
        else if(list.at(5)=="5"){
            ui->Subclass->setText("香蕉");
        }
        else {
            ui->Subclass->setText("error");
            ui->Mainclass->setText("error");
        }

    }
    else if(list.at(4)=="3")
    {
        ui->Mainclass->setText("交通工具");
        if(list.at(5)=="1"){
            ui->Subclass->setText("火车");
        }
        else if (list.at(5)=="2") {
            ui->Subclass->setText("轮船");
        }
        else if (list.at(5)=="3") {
            ui->Subclass->setText("飞机");
        }
        else if (list.at(5)=="0") {
            ui->Subclass->setText("没有目标");
        }
        else if(list.at(5)=="4"){
            ui->Subclass->setText("小轿车");
        }
        else if(list.at(5)=="5"){
            ui->Subclass->setText("香蕉");
        }
        else {
            ui->Subclass->setText("error");
            ui->Mainclass->setText("error");
        }
    }
    else if(list.at(4)=="0"){
        ui->Subclass->setText("正在搜索");
        ui->Mainclass->setText("正在搜索");
    }
    else if(list.at(4)=="9"){
        ui->Subclass->setText("正在搬运");
        ui->Mainclass->setText("正在搬运");
    }
    else {
        ui->Subclass->setText("error");
        ui->Mainclass->setText("error");
    }
}


void MainWindow::addBorder()
{
    int count = 0;
    QString fileName;
    QStringList filePath_list = QFileDialog::getOpenFileNames(this,"Open","选择你要打开的文件","Images (*.png *.xpm *.jpg *.bmp *.jpeg *.pbm *.pgm *.ppm *.pxm *.pnm *.jp2 *.tiff, *.tif ) ");
    QString filePath = QFileDialog::getExistingDirectory(
                this, "选择保存路径",
                "/");
    for(int i=0;i<filePath_list.size();i++)
    {
        Mat image = imread(filePath_list[i].toStdString()),disk;
        cv::copyMakeBorder(image,disk,image.rows*0.1,image.rows*0.1,image.rows*0.1,image.rows*0.1,cv::BORDER_CONSTANT,Scalar(0, 255, 255));
 //       imshow("2",disk);
        fileName = QString("/%1.jpg").arg(count);
        String fileRestore = filePath.toStdString()+fileName.toStdString();
        cv::imwrite(fileRestore,disk);
        count++;
    }
    QMessageBox::information(this,"提示:","图片保存成功！请确保图片实际大小为10cm,否则边框大小不为实际的1cm");
    filePath_list.clear();//清空列表

}
